package com.hp.airmap.common;

import java.lang.ref.Reference;
import java.lang.ref.WeakReference;

/**
 * 创建时间： 2017/1/19
 * 作者：胡鹏
 * 用途：Presenter基类
 */

public abstract class BasePresenter<T> {

    protected Reference<T> mViewRef;//View接口类型的弱引用
    protected SubscriptionWrapper mWrapper;

    public void attachView(T view){
        mViewRef=new WeakReference<T>(view);//建立关联
        mWrapper=new SubscriptionWrapper();
    }

    protected T getView(){
        return mViewRef.get();
    }

    public boolean isViewAttached(){
        return mViewRef!=null&&mViewRef.get()!=null;
    }

    public void detachView(){
        if (mWrapper!=null){
            mWrapper.cancelAll();
            mWrapper=null;
        }
        if (mViewRef!=null){
            mViewRef.clear();
            mViewRef=null;
        }
    }
}
